package com.example.demo.service.impl;

import com.example.demo.entity.maintenance;
import com.example.demo.mapper.maintenanceMapper;
import com.example.demo.mapper.querymanageMapper;
import com.example.demo.service.MaintenanceService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class MaintenanceServiceImpl implements MaintenanceService {
    @Resource
    private maintenanceMapper maintenanceMapper;
    @Resource
    private querymanageMapper querymanageMapper;

    @Override
    public List query()
    {return maintenanceMapper.query();}//查询维修信息

    @Override
    public String insert(@RequestBody maintenance maintenance) {//插入维修信息
        if (maintenanceMapper.selectfkey(maintenance.getFkey()) != null)//检查故障是否维修
            return "故障已维修";
        else
        {
            DateFormat dateFormat = new SimpleDateFormat("yyyyMM");//获取当前时间
            Date date = new Date();
            int num1 = 1;//初始化工作票编号
            if (maintenanceMapper.last() != null) {//检查上一条工作票
                String last = maintenanceMapper.last().getWticketkey();//获取上一条工作票
                String lastdate = last.substring(2, 8);//截取工作票日期
                int num = Integer.parseInt(last.substring(9, 11));//截取工作票编号
                if (dateFormat.format(date).equals(lastdate))//判断当日期与上一张工作票日期是否相等
                    num1 = num + 1;//若相等编号加一
            }
            String wticketkey = "wx" + dateFormat.format(date) + String.format("%03d", num1);//生成工作票
            maintenance.setWticketkey(wticketkey);//设定工作票
            if (maintenanceMapper.insert(maintenance) == 1)//插入维修信息
            {
                querymanageMapper.updateRtime(maintenance.getFkey(),maintenance.getRdate());//更新查询表维修时间
                return maintenance.getWticketkey();
            }
            else return "维修单插入失败";
        }
    }
}
